package cn.iocoder.yudao.module.call.framework.client.impl.box;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.iocoder.yudao.module.call.api.client.dto.*;
import cn.iocoder.yudao.module.call.framework.client.impl.AbstractCallClient;
import cn.iocoder.yudao.module.call.framework.client.property.CallProperties;
import cn.iocoder.yudao.module.call.enums.CallResultTypeEnum;
import cn.iocoder.yudao.module.call.utils.TimeUtil;

public class BoxClient extends AbstractCallClient {

    static final String url = "https://api.33e9cloud.com/2016-01-01/Accounts/";

    static final String callMethod = "/MaskNumberAX/doubleCall";

    @Override
    protected void doInit() {

    }

    public BoxClient(CallProperties properties) {
        super(properties);
        // check config properties not null
    }

    @Override
    public CallRespDTO handleCall(CallReqDTO callReqDTO) {
        return CallRespDTO.ofOk();
    }



    @Override
    public AgentRespDTO handleAgent(AgentReqDTO agentReqDTO) {
        return null;
    }

    @Override
    public CallNotifyDTO handleNotify(JSONObject jsonObject) {

        if (jsonObject.getStr("type").equals("calllog")) {
            JSONObject body = jsonObject.getJSONObject("content");
            CallNotifyDTO callNotifyDTO =  new CallNotifyDTO();
            Integer seconds = body.getInt("duration");
            // 录音下载地址
            callNotifyDTO.setTempRecordUrl(null);
            callNotifyDTO.setCallSecond(seconds);
            callNotifyDTO.setCallMinute(TimeUtil.secondToMin(seconds));
            if (seconds > 0) {
                callNotifyDTO.setResultCode("success");
                callNotifyDTO.setResultType(CallResultTypeEnum.SUCCESS.getValue());
                callNotifyDTO.setResultMsg(CallResultTypeEnum.SUCCESS.getMsg());
            } else {
                callNotifyDTO.setResultCode("fail");
                callNotifyDTO.setResultType(CallResultTypeEnum.FAIL.getValue());
                callNotifyDTO.setResultMsg(CallResultTypeEnum.FAIL.getMsg());
            }
            // 呼叫时间的计算
            callNotifyDTO.setStartTime(DateUtil.date(body.getLong("startTime")).toLocalDateTime());
            callNotifyDTO.setEndTime(DateUtil.date(body.getLong("endTime")).toLocalDateTime());
            // 三三订单号
            callNotifyDTO.setUserData(body.getStr("callId"));
            return callNotifyDTO;
        }


        return null;
    }

    @Override
    public boolean createAgent(AgentReqDTO agentReqDTO) {
        return true;
    }

    @Override
    public JSONObject getAccountInfo(String agent) {
        return null;
    }
}
